<?php

class Admin_Form_ValidateCategory {
	
	// CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	// MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected $_arrData;
	
	public function __construct($arrParam = array(), $options = null) {

		// ========================================
		// KIEM TRA category_name
		// ========================================
		
		if ($arrParam ['action'] == 'add') {
			$options = array ('table' => 'product_category', 'field' => 'name' );
		} else if ($arrParam ['action'] == 'edit') {
			$clause = ' id !=' . $arrParam ['id'];
			$options = array ('table' => 'product_category', 'field' => 'name', 'exclude' => $clause );
		}
		
		$validator = new Zend_Validate ();
		
		$validator->addValidator ( new Zend_Validate_NotEmpty (), true )
				  ->addValidator ( new Zend_Validate_StringLength ( 2, 32 ), true )
				  ->addValidator ( new Zend_Validate_Db_NoRecordExists ( $options ), true );
		if (! $validator->isValid ( $arrParam ['name'] )) {
			$message = $validator->getMessages ();
			$this->_messagesError ['name'] = 'Category name: ' . current ( $message );
			$arrParam ['name'] = '';
		}
		
		
		
		// ========================================
		// KIEM TRA Picture
		// ========================================
		$upload = new Zend_File_Transfer_Adapter_Http ();
		$fileInfo = $upload->getFileInfo ( 'picture' );
		$fileName = $fileInfo ['picture'] ['name'];
		
		if (! empty ( $fileName )) {
			$upload->addValidator ( 'Extension', true, array ('jpg', 'gif', 'png' ), 'picture' );
			$upload->addValidator ( 'Size', true, array ('min' => '2KB', 'max' => '1000KB' ), 'picture' );
			if (! $upload->isValid ( 'picture' )) {
				$message = $upload->getMessages ();
				$this->_messagesError ['picture'] = 'Picture: ' . current ( $message );
					
			}
		}
		// ========================================
		// KIEM TRA Order
		// ========================================
		$validator = new Zend_Validate ();
		$validator->addValidator ( new Zend_Validate_NotEmpty (), true )->addValidator(new Zend_Validate_Int(), true);
		if (! $validator->isValid ( $arrParam ['order'] )) {
			$message = $validator->getMessages ();
			$this->_messagesError ['order'] = 'Order: ' . current ( $message );
			$arrParam ['order'] = '';
		}
		
		// ========================================
		// KIEM TRA status
		// ========================================
		if (empty ( $arrParam ['status'] ) || ! isset ( $arrParam ['status'] )) {
			$arrParam ['status'] = 0;
		}
		
		$this->_arrData = $arrParam;
	}
	
	
	// Kiem tra Error
	// return true neu co loi xuat hien
	public function isError() {
		if (count ( $this->_messagesError ) > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	// Tra ve mot mang cac loi
	public function getMessageError() {
		return $this->_messagesError;
	}
	
	// Tra ve mot mang du lieu sau khi kiem tra
	public function getData($options = null) {
		if ($options ['upload'] == true) {
			$this->_arrData ['picture'] = $this->uploadFile ();
		}
		return $this->_arrData;
	}
	
	public function uploadFile() {
		// Duong dan den thu muc upload
		$upload_dir = UPLOADS_PATH . '/categories';
	
		// ========================================
		// UPLOAD FILE $user_avatar
		// ========================================
	
		$upload = new Zendvn_File_Upload ();
		$fileInfo = $upload->getFileInfo ( 'picture' );
		$fileName = $fileInfo ['picture'] ['name'];
	
		if (! empty ( $fileName )) {
			$fileName = $upload->upload ( 'picture', $upload_dir . '/orignal', array ('task' => 'rename' ), 'cat_' );
			$thumb = Zendvn_File_Images::create ( $upload_dir . '/orignal/' . $fileName );
			$thumb->resize ( 100, 100 )->save ( $upload_dir . '/img100x100/' . $fileName );
			
			$thumb = Zendvn_File_Images::create ( $upload_dir . '/orignal/' . $fileName );
			$thumb->resize ( 450, 450 )->save ( $upload_dir . '/img450x450/' . $fileName );
				
			if($this->_arrData['action'] == 'edit'){
				$upload->removeFile($upload_dir . '/orignal/' . $this->_arrData['current_picture']);
				$upload->removeFile($upload_dir . '/img100x100/' . $this->_arrData['current_picture']);
				$upload->removeFile($upload_dir . '/img450x450/' . $this->_arrData['current_picture']);
			}	
				
		}else{
			if($this->_arrData['action'] == 'edit'){
				$fileName = $this->_arrData['current_picture'];
			}
		}
		return $fileName;
	}
}